package Classes.GameDatas// ActionScript file
{
	import Classes.GameErrors.GameError;
	import Classes.GameUtils.GD_deal;
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameViews.ViewIcons.VI_VectorIcon;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	
	import UIComponents.IMission;
	import UIComponents.IUseItem;
	
	import flash.display.DisplayObject;

	public class GD_Mission extends GameData implements IMission
	{
		private static var _missionsConfigObj:Object;
		/**
		 * 收藏物品对象 
		 */	
		private static var _missionCollectArr:Array;
		/**
		 * 任务的序列号 
		 */		
		private var _serverID:Number
		/**
		 *任务ID 
		 */		
		private var _missionType:Number;
		private var _missionConfigObj:Object;
		
		private var _countsAry:Array;
		private var _missionState:String;
		private var _icon:ViewIcon;
		/**
		 * 任务类型 
		 */	
		private var _taskStyle:Number;
		
		//任务状态 0 表示未领取的 1 表示已领取的任务 
		private var _missionStatue:int;
		
		//步骤数
		private var _missionStep:Number;
		
		/**
		 * 创建 GD_Mission 时 传过来的参数（具体的任务对象）
		 */		
		private static var _createObj:Object;
		public function GD_Mission(missionType:Number):void
		{
			this._missionType=missionType;
			this.init();
		}
		
		private function init():void
		{
			this._missionConfigObj=missionsConfigObj[this.missionType];
			this._taskStyle=this._missionConfigObj.zt_task_type;
			
			//给nowValue动态赋值
			if(null!=_createObj)
			{
				var tmpCountsAry:Array=(_createObj.countsAry) as Array;
				if(null != tmpCountsAry && tmpCountsAry.length>0)
				{
					var len:int=tmpCountsAry.length;
					for(var i:int=0;i<len;i++)
					{
						_serverID=tmpCountsAry[i]["countSerial"];
						var tmp_zt_nowValue:int=tmpCountsAry[i]["nowValue"];
						this.setMissionNowValue(_serverID,tmp_zt_nowValue);
					}
					_createObj=null;
				}
			}
		}
		
		/**
		 * 给nowValue动态赋值 
		 * @param zt_id 任务的序列ID
		 * @param zt_nowValue 任务已经完成的次数
		 * @param isDoingState 是否需要正在进行的标志
		 */		
		public function setMissionNowValue(zt_id:Number,zt_nowValue:int,isDoingState:Boolean=true):void
		{
			var oldTargetValue:int=(this.conditionCounts[zt_id]).targetValue;
			if((this.conditionCounts[zt_id]).countThirdType==105)
			{
				oldTargetValue=(this.conditionCounts[zt_id]).needItemId;//土地等级 在此处
			}
			if(zt_nowValue>=oldTargetValue)
			{
				(this.conditionCounts[zt_id]).nowValue=oldTargetValue;
			}else{
				(this.conditionCounts[zt_id]).nowValue=zt_nowValue;
			}
			
			//专门对土地登记
			if((this.conditionCounts[zt_id]).countThirdType==105)
			{
				if(zt_nowValue<oldTargetValue)
				{
					zt_nowValue=0;
				}else{
					zt_nowValue=1;
				}
				(this.conditionCounts[zt_id]).nowValue=zt_nowValue;
			}
				
			if(this.missionStatue==0)
			{
				this.missionState="new";
			}else if(this.checkMissionIsOver()){
				this.missionState="over";
			}else{
				if(zt_nowValue>0 && isDoingState && zt_nowValue<=oldTargetValue)
				{
					this.missionState="doing";
				}
				
			}
		}
		
		/**
		 * 检测任务是否完成 
		 * @return 
		 */		
		public function checkMissionIsOver():Boolean
		{
			var flag:Boolean=true;
			for each(var count:MissionCount in this.conditionCounts)
			{
				if(count.countThirdType!=105 && count.nowValue<count.targetValue)
				{
					flag=false;
					break;
				}else if(count.countThirdType==105 && count.nowValue<1)
				{
					flag=false;
					break;
				}
			}
			return flag;
		}
		
		/**
		 *  任务类型  101= 提交金币   102 = 提交物品
		 * @return 
		 * 
		 */		
		public function getZt_task_condition_type():Number
		{
			return (this.conditionCounts[this.serverID]).countThirdType;
		}
		
		
		public function getNeedItemId():Number
		{
			return (this.conditionCounts[this.serverID]).needItemId;
		}
		
		public static function creat(creatObj:Object):GD_Mission
		{
			_createObj=null;
			_createObj=creatObj;
			var mission:GD_Mission=new GD_Mission(creatObj.missionID);
			mission.missionStatue=creatObj.missionStatue;
			mission.missionStep =mission._missionConfigObj.zt_task_step;
			return mission;
		}
		
		public function creatTmpIcon():ViewIcon
		{
			return ViewIcon.creat({sourceName:this.sourceName,picName:this.picName});
		}
		
		public function finishHandler(isUpBuild:Boolean=false):void
		{
			if(int(conditionCounts[_serverID].nowValue)!=int(conditionCounts[_serverID].targetValue))
			{
				GD_Player.instance.canNotFinishMission();
			}else{
				var msgContent:Object={missionID:this._missionType,sign:1};
				M_Communication.instance.send(12200,msgContent);
			}
		}
		


		/*getter & setter*/
		public function get missionShowIcon():DisplayObject
		{
			return this.icon;
		}
		
		public function get serverID():Number
		{
			return _serverID;
		}

		/**
		 *任务ID 
		 */
		public function get missionType():Number
		{
			return _missionType;
		}

		/**
		 * 获取任务完成度的图标值 
		 * @return 
		 */		
		public function get missionState():String
		{
			return _missionState;
		}
		
		/**
		 * 设置任务完成度的的图标值 
		 * @param value
		 */		
		public function set missionState(value:String):void
		{
			_missionState=value;
			this.touchObserve("missionState");
		}
		
		public function get sourceName():String
		{
			return this.missionConfigObj.zt_task_source;
		}
		
		public function get picName():String
		{
			return this.missionConfigObj.zt_task_icon;
		}

		public function get icon():ViewIcon
		{
			if(!_icon)
			{
				_icon=ViewIcon.creat({dataTarget:this,creatType:"mission"});
				_icon.scaleX=0.6;
				_icon.scaleY=0.6;
			}
			return _icon;
		}
		
		public  function get npcIcon():DisplayObject
		{
			var sourceName:String=this.missionConfigObj.zt_task_npc_source;
			var picName:String=this.missionConfigObj.zt_task_npc_icon;
			var iconDis:DisplayObject=VI_VectorIcon.creat({sourceName:sourceName,picName:picName}) as DisplayObject;
			return iconDis;
		}
		
		public function get missionName():String
		{
			return this.missionConfigObj.zt_task_name;
		}
		
		public function get missionDescription():String
		{
			return this.missionConfigObj.zt_task_description;
		}
		
		public function get conditionCountsAry():Array
		{
			var returnAry:Array=new Array;
			for each(var count:MissionCount in this.conditionCounts)
			{
				var countObj:Object={countType:count.countType,description:count.description,nowValue:count.nowValue,targetValue:count.targetValue,needItemId:count.needItemId,countSecondType:count.countThirdType};
				returnAry.push(countObj);
			}
//			returnAry.sortOn("countType",Array.NUMERIC);
			return returnAry;
		}
		
		public function get conditionCounts():Object
		{
			return this.missionConfigObj.conditionCounts;
		}
		
		/**
		 * 获取特殊的任务奖励 
		 * @return 
		 */		
		public function get rewardSpectialCountsAry():Array
		{
			var returnAry:Array=new Array;
			
			var tmpObj:Object=new Object();
			for each(var mc:MissionCount in rewardCounts)
			{
				tmpObj[mc.getPar(6)]=mc;
			}
			for each(var count:MissionCount in tmpObj)
			{
				switch(count.countThirdType)
				{
					case 11://对话
					case 12://动画
					case 13://锁屏移动屏幕
					case 15://进度
						var countObj:Object={countType:count.countType,par3:count.getPar(3),par2:count.getPar(2),par1:count.getPar(1),countSecondType:count.countThirdType,par4:count.getPar(4),par5:count.getPar(5),par6:count.getPar(6)};
						returnAry.push(countObj);
						break;
				}
			}
			return returnAry;
		}
		
		public function get rewardCountsAry():Array
		{
			//return new Array;
			var returnAry:Array=new Array;
			for each(var count:MissionCount in this.rewardCounts)
			{
				var countPic:ViewIcon
				switch(count.countThirdType)
				{
					case 1://金币
						countPic=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_JB",creatNum:count.targetValue});
						countPic.displayViewIconName=Language.jb;
						break;
					case 2://物品
						var iuserItem:IUseItem=GD_deal.instance.getDataById(int(count.getPar(1)));
						var vi:ViewIcon=(iuserItem.itemPic) as ViewIcon;
						countPic=ViewIcon.creat({sourceName:vi.sourceName,picName:vi.picName,creatNum:count.targetValue});
						countPic.displayViewIconName=iuserItem.itemName+"："+iuserItem.description;
						break;
					case 3:
						countPic=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_Act",creatNum:count.targetValue});
						countPic.displayViewIconName=Language.act;
						break;
					case 4:
						countPic=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_Exp",creatNum:count.targetValue});
						countPic.displayViewIconName=Language.exp;
						break;
					case 10:
						countPic=ViewIcon.creat({sourceName:"icon0.swf",picName:"Icon_OB",creatNum:count.targetValue});
						countPic.displayViewIconName=Language.ob;
						break;
				}
				
				if(null!=countPic)
				{
					countPic.type=1;
					countPic.scaleX=0.5;
					countPic.scaleY=0.5;
					returnAry.push(countPic);
					countPic=null;
				}
			}
			return returnAry;
		}
		
		public function get rewardCounts():Object
		{
			return this.missionConfigObj.rewardCounts;
		}
		
		public function get otherCountsAry():Array
		{
			var otherObj:Object=this.missionConfigObj.otherCounts;
			
			var tmpObj:Object=new Object();
			for each(var mc:MissionCount in otherObj)
			{
				tmpObj[mc.getPar(6)]=mc;
			}
			var returnAry:Array=new Array;
			for each(var count:MissionCount in tmpObj)
			{
				var countObj:Object={countType:count.countType,par3:count.getPar(3),par2:count.getPar(2),par1:count.getPar(1),countSecondType:count.countThirdType,par4:count.getPar(4),par5:count.getPar(5),par6:count.getPar(6)};
				returnAry.push(countObj);
			}
//			returnAry.sortOn("countType",Array.NUMERIC);
			return returnAry;
		}
		
		public function get missionTip():String
		{
//			return this.missionConfigObj.zt_task_help;
			return "";
		}

		public static function get missionsConfigObj():Object
		{
			if(!_missionsConfigObj)
			{
				_missionsConfigObj=new Object;
				_missionCollectArr=new Array();
				var missionsConfigStr:String=M_ConfigData.instance.getCSVByName("任务配置表");
				var missionsConfigAry:Array=GU_DataTransform.csvTransformObjsAry(missionsConfigStr,15);
				for each(var missionConfigObj:Object in missionsConfigAry)
				{
					_missionsConfigObj[missionConfigObj.zt_task_id]=missionConfigObj;
					_missionsConfigObj[missionConfigObj.zt_task_id].conditionCounts=new Object;
					_missionsConfigObj[missionConfigObj.zt_task_id].rewardCounts=new Object;
					_missionsConfigObj[missionConfigObj.zt_task_id].otherCounts=new Object;
					
				
					switch(Number(missionConfigObj.zt_task_type))
					{
						case 10:
						case 11:
						break;
						default :
							_missionCollectArr.push(_missionsConfigObj[missionConfigObj.zt_task_id]);
					}
					
				}
				var countsConfigStr:String=M_ConfigData.instance.getCSVByName("任务计数器配置表");
				var countsConfigAry:Array=GU_DataTransform.csvTransformObjsAry(countsConfigStr,12);
				for each(var countConfigObj:Object in countsConfigAry)
				{
					var missionCount:MissionCount=new MissionCount(countConfigObj);
					var missionType:Number=missionCount.missionType;
					var checkMissionConfigObj:Object=_missionsConfigObj[missionType];
					if(!checkMissionConfigObj)throw new GameError("任务配置表错误，该任务不存在"+missionType);
//					if(!checkMissionConfigObj){trace("任务配置表错误，该任务不存在"+missionType);continue;}
					switch(missionCount.countSecondType)
					{
						case 2://condition
							checkMissionConfigObj.conditionCounts[missionCount.countType]=missionCount;
							break;
						case 3://reward
							checkMissionConfigObj.rewardCounts[missionCount.countType]=missionCount;
							break;
						default://触发
							checkMissionConfigObj.otherCounts[missionCount.countType]=missionCount;
							break;
					}
				}
			}
			return _missionsConfigObj;
		}

		public function get missionConfigObj():Object
		{
			return _missionConfigObj;
		}

		/**
		 * 收藏物品对象 
		 */
		public static function get missionCollectArr():Array
		{
			return _missionCollectArr;
		}

		/**
		 * 任务类型 
		 */
		public function get taskStyle():Number
		{
			return _taskStyle;
		}

		/**
		 * 任务状态 0 表示未领取的 1 表示已领取的任务 
		 */
		public function get missionStatue():int
		{
			return _missionStatue;
		}

		/**
		 * @private
		 */
		public function set missionStatue(value:int):void
		{
			_missionStatue = value;
			if(value==0)
			{
				this.missionState="new";
			}else{
				if(this.checkMissionIsOver())
				{
					this.missionState="over";
				}else{
					this.missionState="";
				}
			}
		}

		/**
		 * 任务 步骤数
		 */
		public function get missionStep():Number
		{
			return this._missionStep;
		}

		public function set missionStep(value:Number):void
		{
			_missionStep = value;
			if(value>0)
			{
				this.touchObserve("missionStep");
			}
		}
	


	}
}
import Classes.GameErrors.GameError;

class MissionCount
{
	private var _countObj:Object;
	private var _nowValue:Number;
	public function MissionCount(countObj:Object):void
	{
		this._countObj=countObj;
//		this.nowValue=countObj.nowValue;
	}
	
	public function getPar(parNum:int):String
	{
		if(parNum<1 || parNum>6)throw new GameError("参数索引错误");
		return this.countObj["zt_task_elf_"+parNum];
	}

	/*getter & setter*/
	/**
	 * 任务ID 
	 * @return 
	 */	
	public function get missionType():Number
	{
		return this.countObj.zt_task_id;
	}
	
	public function get countObj():Object
	{
		return _countObj;
	}
	/**
	 * 任务序列号
	 * @return 
	 */	
	public function get countType():Number
	{
		return this.countObj.zt_id;
	}
	
	/**
	 * 类型ID 触发1 需求2 奖励3 
	 * @return 
	 */	
	public function get countSecondType():Number
	{
		return this.countObj.zt_type_id;
	}
	
	/**
	 * 计数器类型 
	 * @return 
	 */	
	public function get countThirdType():Number
	{
		return this.countObj.zt_task_condition_type;
	}
	
	/**
	 * 需求数目
	 * @return 
	 */	
	public function get targetValue():Number
	{
		return this.countObj.zt_task_value;
	}
	
	public function get description():String
	{
		return this.countObj.zt_task_need_str;
	}

	/**
	 * 当前任务完成的个数 
	 * @return 
	 */	
	public function get nowValue():Number
	{
		return _nowValue;
	}
	
	public function get needItemId():Number
	{
		return this.countObj.zt_task_elf_1;
	}
	
	public function get zt_task_elf_2():Number
	{
		return this.countObj.zt_task_elf_2;
	}

	public function set nowValue(value:Number):void
	{
		_nowValue = value;
	}
}